package com.jyx.iterator.arraylist;

import com.jyx.iterator.collection.Collection;
import com.jyx.iterator.iterator.Iterator;

/**
 * @author  作者: 蒋永星
 * @email       jiangyongxing@myzczk.com
 * @company		www.urbanspace.cn
 * @date 创建时间：2016年5月29日 下午5:00:37 
 * @version 1.0
 * @parameter  
 * @since
 * @return 
 */
public class ArrayList implements Collection {
	
	Object[] objects = new Object[10];
	int index = 0;
	
	public void add(Object o){
		if(index == objects.length){
			Object[] newObject = new Object[objects.length * 2];
			System.arraycopy(objects, 0, newObject,0,objects.length);
			objects = newObject;
		}
		objects[index] = o;
		index ++;
	}
	
	public int size(){
		return index;
	}
	
	public Iterator iterator(){
		return new ArrayListIterator();
	}
	
	private class ArrayListIterator implements Iterator{

		private int currentIndex = 0;
		@Override
		public Object Next() {
			Object o = objects[currentIndex];
			currentIndex++;
			return o;
		}

		@Override
		public boolean hasNext() {
			if(currentIndex >= index) return false;
			return true;
		}
		
	}
	
}
